package whf.shop.wrapper;

import java.io.IOException;
import java.io.Writer;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;

import whf.framework.file.wraper.FileDisplayWrapper;
import whf.framework.util.Pair;
import whf.framework.util.StringUtils;
import whf.framework.web.tag.TableColumn;
import whf.framework.web.tag.TableRow;
import whf.framework.web.tag.wrapper.IDisplayWrapper;
import whf.shop.entity.Product;
import whf.shop.utils.ShopUtils;

/**
 * @author wanghaifeng
 * @create Mar 30, 2007 9:35:24 PM
 * 
 */
public class ProductDisplayWrapper extends FileDisplayWrapper implements IDisplayWrapper{
	public ProductDisplayWrapper(){
		super("middle", true, defaultProperties);
	}
	
	/* (non-Javadoc)
	 * @see whf.framework.web.tag.wrapper.IDisplayWrapper#writeHtml(javax.servlet.jsp.PageContext, java.util.List, whf.framework.web.tag.TableRow)
	 */
	public void writeHtml(PageContext pageContext, List<TableColumn> displayColumns, TableRow row) throws JspException, IOException {
		HttpServletRequest req = (HttpServletRequest)pageContext.getRequest();
		Writer writer = pageContext.getOut();
		Product product = (Product)row.getRowObject();
		
		Pair<String, String> urls = this.getFileURI(pageContext, product);
		String url = urls.getValue();

		writer.write("<table width='100%' border='0' cellspacing='0' cellpadding='0'>\n");
		writer.write("<tr><td colspan=2>");
		writer.write("<a href='"+ req.getContextPath() +"/shop/Order.do?webMethod=viewMyCart&detailProductId="+product.getId()+"'>");
		writer.write("<img src='"+url+"' border='0'>");
		writer.write("</a></td></tr>");
		
		writer.write("<tr><td width='30%' nowrap>名称</td><td width='*%'>" + (product.getName() == null?"":product.getName()) +"</td></tr>\n");
		//writer.write("<tr><td width='30%' nowrap>说明</td><td width='*%'>" + (product.getRemarks() == null?"": product.getRemarks()) +"</td></tr>\n");
		writer.write("<tr><td width='30%' nowrap>单价</td><td width='*%'>" + ShopUtils.formatPrice(pageContext.getSession(), product.getPrice()).getValue() +"</td></tr>\n");
		writer.write("<tr><td width='30%' nowrap>折扣</td><td width='*%'>" + StringUtils.format( (1-product.getDiscount()) * 10, "0.0") +"</td></tr>\n");
		writer.write("<tr><td colspan='2'><a href='javascript:addToCart("+product.getId()+")'>加入购物车</a></td></tr>\n");
		writer.write("</table>\n");
	}

}
